<?php
namespace Home\Controller;
header('content-type:text/html;charset=utf-8');
use Think\Controller;
use Org\Type\CatTree;
class GoodslistController extends Controller {
    public function index(){

        //实例化商品分类
        $cats=M('Cats');
        //查询所有类别
        $allcats=$cats->field('catsid,catsname,pid,path')->select();

        //调用分类函数
        $type = CatTree::getlist($allcats);
        //为模板变量赋值
        $this->assign('type',$type);

        //实例化商品信息
        $good=M('Goods');

       //接受用户点击的顶级分类id
        $catsid='';
        $catsid=I('get.catsid');

        //接受用户点击的子分类catsname
        $catsname='';
        $catsname=I('get.catsname');

        //接受用户点击的子分类soncatsid
        $soncatsid='';
        $soncatsid=I('get.soncatsid');


        if($catsid){
            $ucats=$cats->field('catsname')->where('pid='.$catsid)->select();  
        }else{
            $ucats['0']['catsname']=$catsname;
        }
        

        //分配到模板
        $this->assign('ucats',$ucats);

        // 查询用户点击分类的所有商品
        if($catsid){
           $ugoods=$good->where('gcats='.$catsid)->select(); 
       }else{
           $ugoods=$good->where('pcats="'.$soncatsid.'"')->select();
       }
        

        $this->assign('ugoods',$ugoods);

    	$this->display();

    }

    //状态排序
      public function state(){

          //实例化商品分类
          $cats=M('Cats');
          //查询所有类别
          $allcats=$cats->field('catsid,catsname,pid,path')->select();
          //调用分类函数
          $type = CatTree::getlist($allcats);
          //为模板变量赋值
          $this->assign('type',$type);

          $this->display();
      }
}